import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { list as deviceTypeList } from '../deviceType/deviceType.api';
import { getDeviceModelByType } from '../deviceModel/deviceModel.api';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { buildUUID } from '/@/utils/uuid';

export const columns: BasicColumn[] = [
  {
    title: 'PLC名称',
    dataIndex: 'name',
    width: 120,
  },
  {
    title: 'PLC编号',
    dataIndex: 'deviceSn',
    width: 100,
  },
  {
    title: 'PLC类型',
    dataIndex: 'deviceTypeId',
    width: 120,
  },
  {
    title: 'PLC型号',
    dataIndex: 'model',
    width: 80,
  },
  {
    title: 'IP',
    dataIndex: 'ip',
    width: 100,
  },
  {
    title: '端口',
    dataIndex: 'port',
    width: 100,
  },
  {
    title: '设备状态',
    dataIndex: 'status',
    width: 80,
    slots: { customRender: 'status' },
    // customRender: ({ text }) => {
    //   return render.renderDict(text, 'iot_device_status');
    // },
  },
  {
    title: '上/下线时间',
    dataIndex: 'switchTime',
    width: 100,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: 'PLC名称',
    field: 'name',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: 'PLC编号',
    field: 'deviceSn',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: 'PLC类型',
    field: 'deviceTypeId',
    component: 'ApiTreeSelect',
    componentProps: {
      api: deviceTypeList,
      params: {alias: 'PLC'},
      immediate: true,
      expandAll: true,
      resultField: 'data',
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      dropdownStyle: {
        maxHeight: '50vh',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    label: 'PLC状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'iot_device_status',
      placeholder: '请选择PLC状态',
      stringToNumber: false,
    },
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: 'PLC名称',
    field: 'name',
    component: 'Input',
    required: true,
  },
  {
    label: 'PLC编号',
    field: 'deviceSn',
    component: 'Input',
    required: true,
  },
  {
    label: 'IP地址',
    field: 'ip',
    component: 'Input',
    required: true,
  },
  {
    label: 'PLC端口',
    field: 'port',
    component: 'InputNumber',
    componentProps: {
      //步数
      step: 1,
    },
  },
  {
    label: 'PLC类型',
    field: 'deviceTypeId',
    component: 'ApiTreeSelect',
    required: true,
    componentProps: {
      api: deviceTypeList,
      params: {alias: 'PLC'},
      immediate: true,
      expandAll: true,
      resultField: 'data',
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      dropdownStyle: {
        maxHeight: '50vh',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    label: '设备型号',
    field: 'model',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        api: getDeviceModelByType,
        params: {typeId: formModel.deviceTypeId},
        labelField: 'model',
        valueField: 'id',
        getPopupContainer: () => document.body,
      };
    },
  },  
];